package com.tiger.cloud.service.share;

import com.alibaba.fastjson.JSON;

import com.tiger.cloud.common.mybatis.MybatisTigerDao;
import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.common.sms.request.SmsSendRequest;
import com.tiger.cloud.common.sms.response.SmsSendResponse;
import com.tiger.cloud.common.sms.util.ChuangLanSmsUtil;
import com.tiger.cloud.dao.share.ShareSmsDao;
import com.tiger.cloud.enums.AppSmsSendStatus;
import com.tiger.cloud.enums.AppSmsType;
import com.tiger.cloud.model.share.ShareSms;
import com.tiger.cloud.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;

/**
 * crm分享的验证码
 */
@Service("shareSmsService")
public class ShareSmsService extends TigerBaseService<ShareSms> implements IShareSmsService {

    @Autowired
    private ShareSmsDao shareSmsDao;

    @Override
    public MybatisTigerDao<ShareSms> getEntityDao() {
        return shareSmsDao;
    }


    public static final String charset = "utf-8";
    public static String account = "N1421116";
    public static String pswd = "YjXcvnfx911ed4";
    public static String smsSingleRequestServerUrl = "http://smssh1.253.com/msg/send/json";

    /**
     * 这里发送的是没有token的这类用户短信
     *
     * @param msg
     * @param phone
     * @param appSmsType
     * @param activeTime
     * @return
     */
    @Override
    public ShareSms sendToUID(String msg, String code, String phone, AppSmsType appSmsType, Date activeTime) {
        ShareSms shareSms = new ShareSms();
        shareSms.setMessage(msg);
        AppSmsSendStatus sendStatus = AppSmsSendStatus.FAIL;
        try {
            SmsSendResponse sendResponse = sendSms(msg, phone);
            if (StringUtils.isEmpty(sendResponse.getCode()) && sendResponse.getCode().equals("0")) {
                sendStatus = AppSmsSendStatus.SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (appSmsType != null) {
            shareSms.setType(appSmsType.getCode());
        }
//        shareSms.setActiveTime(activeTime);
        //date转timestamp
        String s = DateUtil.formatCare2Second(activeTime);
        Timestamp timestamp = Timestamp.valueOf(s);

        shareSms.setActiveTime(timestamp);
        shareSms.setSendStatus(sendStatus.getCode());
        shareSms.setPhone(phone);
        shareSms.setCode(code);
        int insert = shareSmsDao.insert(shareSms);
        if (insert > 0) {
            return shareSms;
        }
        return null;
    }


    private SmsSendResponse sendSms(String content, String phone) {
        // 短信内容
        String report = "true";
        SmsSendRequest smsSingleRequest = new SmsSendRequest(account, pswd, content, phone, report);
        String requestJson = JSON.toJSONString(smsSingleRequest);
        System.out.println("before request string is: " + requestJson);
        String response = ChuangLanSmsUtil.sendSmsByPost(smsSingleRequestServerUrl, requestJson);
        System.out.println("response after request result is :" + response);
        SmsSendResponse smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);
        System.out.println("response  toString is :" + smsSingleResponse);
        return smsSingleResponse;
    }
}